package com.geli.smarttools.activity;

import android.content.Context;
import android.graphics.Typeface;
import android.hardware.ConsumerIrManager;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.geli.smarttools.R;
import com.geli.smarttools.bean.SettingBean;
import com.geli.smarttools.tool.PackTool;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private PackTool packTool ;
    /**
     * 是否有红外功能
     */
    private boolean isSupportIr;
    private SettingBean settingBean;
    private TextView tempShow, airWindDir, windDirAuto, windSpeed, modeShow;
    private ImageView modeCold, modeWatted, modeAuto, modeSupply, modeHeating;
    List<View> imageViewList =new ArrayList<>() ;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar!=null){
            actionBar.hide();
        }

        setContentView(R.layout.activity_index);

        inItEvent(this);
        inItUI( this);
    }


    private void inItEvent(Context context) {
        ConsumerIrManager consumerIrManager = (ConsumerIrManager) context.getSystemService(CONSUMER_IR_SERVICE);
        isSupportIr = consumerIrManager != null && consumerIrManager.hasIrEmitter();
        if (!isSupportIr) {
            showToast("设备不支持红外功能");
        } else {
            showToast("红外设备就绪");
        }

        settingBean = new SettingBean(0, 25, 0, 0, 0, 0);
        packTool =new PackTool(consumerIrManager) ;
    }


    private void inItUI( Context context) {
        findViewById(R.id.btn_air_power).setOnClickListener(this);
        findViewById(R.id.btn_air_mode).setOnClickListener(this);
        findViewById(R.id.btn_air_up).setOnClickListener(this);
        findViewById(R.id.btn_air_down).setOnClickListener(this);
        findViewById(R.id.btn_air_auto).setOnClickListener(this);
        findViewById(R.id.btn_air_count).setOnClickListener(this);
        findViewById(R.id.btn_air_direction).setOnClickListener(this);

        //显示设置
        Typeface type = Typeface.createFromAsset(context.getAssets(), "fonts/lcd.TTF");
        tempShow = (TextView) findViewById(R.id.temp_show);
        tempShow.setTypeface(type);

        modeShow = (TextView) findViewById(R.id.text_mode);
        windSpeed = (TextView) findViewById(R.id.text_wind_speed);
        airWindDir = (TextView) findViewById(R.id.wind_dir);
        windDirAuto =(TextView)  findViewById(R.id.wind_dir_auto);

        modeCold = (ImageView) findViewById(R.id.image_cold);
        modeWatted =(ImageView)  findViewById(R.id.image_watted);
        modeAuto = (ImageView) findViewById(R.id.image_auto);
        modeSupply = (ImageView) findViewById(R.id.image_supply);
        modeHeating = (ImageView) findViewById(R.id.image_heating);


        // 顺序不能乱
        imageViewList.add(modeAuto) ;
        imageViewList.add(modeCold) ;
        imageViewList.add(modeWatted) ;
        imageViewList.add(modeSupply) ;
        imageViewList.add(modeHeating) ;
    }

    private void  showToast(String text) {
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        if(!isSupportIr){
            showToast("设备不支持红外功能");
            return;
        }

        if (settingBean.isPowerOff() && v.getId() != R.id.btn_air_power) {
            showToast("请先开机");
            return;
        }


        switch (v.getId()) {
            case R.id.btn_air_mode: // 模式
                int mode = settingBean.getmMode();
                mode++;
                if (mode > 4) {
                    mode = 0;
                }
                settingBean.setmMode(mode);
                break;

            case R.id.btn_air_power: // 开关
                if (settingBean.getmPower() == 0) {
                    settingBean.setmPower(1);
                } else {
                    settingBean.setmPower(0);
                }
                break;

            case R.id.btn_air_up: // 温度加
                int temp  = settingBean.getmTmp();
                temp++;
                if (temp > 30) {
                    temp = 16;
                }
                settingBean.setmTmp(temp);
                break;

            case R.id.btn_air_down: // 温度减
                int tempDown = settingBean.getmTmp();
                tempDown--;
                if (tempDown < 16) {
                    tempDown = 30;
                }
                settingBean.setmTmp(tempDown);
                break;

            case R.id.btn_air_auto: // 自动or手动
                if (settingBean.getmenergy() == 0) {
                    settingBean.setmenergy(1);
                } else {
                    settingBean.setmenergy(0);
                }
                break;

            case R.id.btn_air_count: // 风量
                int windCount   = settingBean.getmWindCount();
                windCount++;
                if (windCount > 3) {
                    windCount = 0;
                }
                settingBean.setmWindCount(windCount);
                break;

            case R.id.btn_air_direction: // 风向
                int airDirection = settingBean.getmWindDir();
                airDirection++;
                if (airDirection > 3) {
                    airDirection = 0;
                }
                settingBean.setmWindDir(airDirection);
                break;
            default:
                break;
        }

        packTool.sendCmdPack(settingBean);
        updataAirUI(settingBean); // onClick
    }


    @Override
    protected void onResume() {
        super.onResume();
        updataAirUI(settingBean);
    }



    private void changeImageState(SettingBean airbean){
        int state = airbean.getmMode() ;
        for(int i=0 ; i< imageViewList.size() ;i++){
            if(i==state){
                imageViewList.get(i).setVisibility(View.VISIBLE);
            }else{
                imageViewList.get(i).setVisibility(View.INVISIBLE);
            }
        }
        modeShow.setText(airbean.getCurrentModeInfo());
        tempShow.setText(airbean.getmTmp() + "℃");
    }


    public void updataAirUI(SettingBean airbean) {
        if(airbean==null){
            return;
        }

        if(airbean.isPowerOff()){
            modeCold.setVisibility(View.INVISIBLE);
            modeWatted.setVisibility(View.INVISIBLE);
            modeAuto.setVisibility(View.INVISIBLE);
            modeSupply.setVisibility(View.INVISIBLE);
            modeHeating.setVisibility(View.INVISIBLE);

            tempShow.setText("-");
            windSpeed.setText("-");
            airWindDir.setText("-");
            windDirAuto.setText("-");
            modeShow.setText("-");
            return;
        }

        changeImageState(airbean);
        windSpeed.setText(airbean.getWindCountInfo());
        airWindDir.setText( airbean.getWindirectionInfo());
        windDirAuto.setText( airbean.getMenergyInfo());

    }


}
